package com.genius.reservation.supplier.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genius.reservation.car.entity.ReservationStartCarInformation;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.common.constant.Constant;
import com.genius.reservation.common.rabbitmq.RabbitMessageEntity;
import com.genius.reservation.mapper.ReservationStartCarInformationMapper;
import com.genius.reservation.mapper.SupplierInformationMapper;
import com.genius.reservation.reservation.entity.ReservationInformation;
import com.genius.reservation.supplier.entity.SupplierInformation;
import com.genius.reservation.supplier.service.ISupplierService;
import com.genius.reservation.user.entity.UserInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierInformationMapper, SupplierInformation> implements ISupplierService  {

    @Autowired
    @Lazy
    SupplierInformationMapper supplierMapper;


    public static Map<String, RabbitMessageEntity> message = new HashMap<>();

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SupplierInformation createSupplier(UserInformation user) {
        LambdaQueryWrapper<SupplierInformation> eq = supplierMapper.getQueryWrapper().eq(SupplierInformation::getSupplierNumber, user.getUserNumber());
        SupplierInformation supplierInformation1 = supplierMapper.selectOne(eq);
        if (ObjectUtil.isEmpty(supplierInformation1)){
            SupplierInformation supplierInformation = new SupplierInformation();
            supplierInformation.setSupplierName(user.getName());
            supplierInformation.setSupplierNumber(user.getUserNumber());
            supplierInformation.setUserNumber(user.getUserNumber());
            return this.saAndUp(supplierInformation);
        }else{
            supplierInformation1.setUserNumber(user.getUserNumber());
            return this.saAndUp(supplierInformation1);
        }
    }

    @Override
    public void newMessage(RabbitMessageEntity obj) {
        synchronized (this) {
            message.put(obj.getReservationNumber(), obj);
        }
    }


    @Override
    public ResponseRule getMessage(String receiveManagerNumber) {
        synchronized (this) {
            ResponseRule<Object> ok;
            if (message.size() != 0) {
                ok = ResponseRule.ok(message.get(receiveManagerNumber).getData());
                message.remove(receiveManagerNumber);
            } else {
                ok = ResponseRule.ok("没有新消息");
            }
            return ok;
        }
    }

    @Override
    public ResponseRule getAllMessage(String userNumber) {
        synchronized (this) {
            ResponseRule<Object> ok = ResponseRule.ok("没有新消息");
            if (message.size() != 0) {
                for(String key:message.keySet()){
                    if (Objects.equals(message.get(key).getData().getSupplierNumber(), userNumber)) {
                        ok =  ResponseRule.ok(message.get(key));
                        message.remove(key);
                    }
                    return ok;
                }
            }
            return ok;
        }
    }

    @Override
    public ResponseRule getAllDriver(String supplierNumber) {

        return null;
    }

    @Override
    public ResponseRule getSupplier(String supplierNumber) {
        LambdaQueryWrapper<SupplierInformation> eq = supplierMapper.getQueryWrapper().eq(SupplierInformation::getSupplierNumber, supplierNumber);
        SupplierInformation supplierInformation = supplierMapper.selectOne(eq);
        return ResponseRule.ok(supplierInformation);
    }


}
